package auctionhaus

import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Customer)
class CustomerTests extends GroovyTestCase {

    void testEmailAddressIsRequired() {
        Customer c = new Customer()
        assert !c.validate()
        assert 'nullable' == c.errors['emailAddress'].code
    }

    void testPasswordIsRequired() {
        Customer c = new Customer()
        assert !c.validate()
        assert 'nullable' == c.errors['password'].code
    }

    //Customer.dateCreated is required, but is tested in CustomerIntegrationTests

    void testEmailAddressValid() {
        Customer c = new Customer(emailAddress: 'test@umn.edu')
        c.validate()
        assert null == c.errors['emailAddress']
    }

    void testEmailAddressInvalid() {
        Customer c = new Customer(emailAddress: 'test')
        assert !c.validate()
        assert 'email.invalid' == c.errors['emailAddress'].code

        c.emailAddress = 'test@'
        assert !c.validate()
        assert 'email.invalid' == c.errors['emailAddress'].code

        c.emailAddress = '@umn.edu'
        assert !c.validate()
        assert 'email.invalid' == c.errors['emailAddress'].code
    }

    void testPasswordLengthTooSmall() {
        Customer  c = new Customer(password: '12345')
        assert !c.validate()
        assert 'size.toosmall' == c.errors['password'].code
        
        c.password = ''
        assert !c.validate() 
        assert 'blank' == c.errors['password'].code
    }

    void testPasswordLengthTooBig() {
        Customer  c = new Customer(password: '123456789')
        assert !c.validate()
        assert 'size.toobig' == c.errors['password'].code
    }

    void testPasswordLengthValid() {
        Customer  c = new Customer(password: '123456')
        c.validate()
        assert null == c.errors['password']

        c.password = '1234567'
        c.validate()
        assert null == c.errors['password']

        c.password = '12345678'
        c.validate()
        assert null == c.errors['password']
    }
}
